#include "gd32e10x_libopt.h"
#include "stdlib.h"
#include "font.h"

void I2cInit(void)
{
	rcu_periph_clock_enable(RCU_I2C0);
	rcu_periph_clock_enable(RCU_GPIOB);
	gpio_init(GPIOB, GPIO_MODE_AF_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_6);//KaiLouFuYongShuChu
	gpio_init(GPIOB, GPIO_MODE_AF_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_7);
  gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_4);//TuiWanShuChu
	gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_5);
  gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_3);//TuiWanShuChu
	OLED_SCL_Set();
  OLED_SDA_Set();
  OLED_RES_Clr();
  OLED_DC_Clr();
  OLED_CS_Clr();
  i2c_clock_config(I2C0, 1000000, I2C_DTCY_2);//IIC0,1K,LT/HT=2
  // I2C address configure 
  i2c_mode_addr_config(I2C0, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, 0x78);
  // enable I2C0 
  i2c_enable(I2C0);
  // enable acknowledge 
  i2c_ack_config(I2C0, I2C_ACK_ENABLE);
}

void WriteCmd(uint8_t num)
{
    /* wait until I2C bus is idle */
    while(i2c_flag_get(I2C0, I2C_FLAG_I2CBSY));
    /* send a start condition to I2C bus */
    i2c_start_on_bus(I2C0);
    /* wait until SBSEND bit is set */
    while(!i2c_flag_get(I2C0, I2C_FLAG_SBSEND));
    /* send slave address to I2C bus */
    i2c_master_addressing(I2C0, 0x78, I2C_TRANSMITTER);
    /* wait until ADDSEND bit is set */
    while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));/*ChengXuKaZaiZheLi*/
    /* clear ADDSEND bit */
    i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
    /* wait until the transmit data buffer is empty */
    while(!i2c_flag_get(I2C0, I2C_FLAG_TBE));
    /* data transmission */
    i2c_data_transmit(I2C0, 0x00);
    /* wait until the TBE bit is set */
    while(!i2c_flag_get(I2C0, I2C_FLAG_TBE));
    /* send a stop condition to I2C bus */
    i2c_data_transmit(I2C0, num);
    /* wait until the TBE bit is set */
    while(!i2c_flag_get(I2C0, I2C_FLAG_TBE));
    /* wait for transfer complete flag */
    while(!i2c_flag_get(I2C0, I2C_FLAG_BTC));
    /* send a stop condition to I2C bus */
    i2c_stop_on_bus(I2C0);
    /* wait until stop condition generate */
    while(I2C_CTL0(I2C0)&0x0200);
}

void WriteData(uint8_t num)
{
    /* wait until I2C bus is idle */
    while(i2c_flag_get(I2C0, I2C_FLAG_I2CBSY));
    /* send a start condition to I2C bus */
    i2c_start_on_bus(I2C0);
    /* wait until SBSEND bit is set */
    while(!i2c_flag_get(I2C0, I2C_FLAG_SBSEND));
    /* send slave address to I2C bus */
    i2c_master_addressing(I2C0, 0x78, I2C_TRANSMITTER);
    /* wait until ADDSEND bit is set */
    while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
    /* clear ADDSEND bit */
    i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
    /* wait until the transmit data buffer is empty */
    while(!i2c_flag_get(I2C0, I2C_FLAG_TBE));
    /* data transmission */
    i2c_data_transmit(I2C0, 0x40);
    /* wait until the TBE bit is set */
    while(!i2c_flag_get(I2C0, I2C_FLAG_TBE));
    /* send a stop condition to I2C bus */
    i2c_data_transmit(I2C0, num);
    /* wait until the TBE bit is set */
    while(!i2c_flag_get(I2C0, I2C_FLAG_TBE));
    /* wait for transfer complete flag */
    while(!i2c_flag_get(I2C0, I2C_FLAG_BTC));
    /* send a stop condition to I2C bus */
    i2c_stop_on_bus(I2C0);
    /* wait until stop condition generate */
    while(I2C_CTL0(I2C0)&0x0200);
}

void OledSetPos(uint8_t x, uint8_t y) 
{ 
  WriteCmd(0xb0+y);             //   0xb0~0xb7
  WriteCmd(((x&0xf0)>>4)|0x10); 
  WriteCmd((x&0x0f));           
} 
 

void OledDisplayOn(void)
{
  WriteCmd(0X8D);  //SET DCDC
  WriteCmd(0X14);  //DCDC ON
  WriteCmd(0XAF);  //DISPLAY ON
}
   
void OledDisplayOff(void)
{
  WriteCmd(0X8D);  //SET DCDC
  WriteCmd(0X10);  //DCDC OFF
  WriteCmd(0XAE);  //DISPLAY OFF
}
 
void OledClear(void)  
{  
  uint8_t i,n;	
  for(i=0;i<8;i++)  
  {  
    WriteCmd (0xb0+i);    
    WriteCmd (0x00);      
    WriteCmd (0x10);      
    for(n=0;n<128;n++)
    WriteData(0); 
  } 
}

void OledShowData(uint8_t x, uint8_t y, uint8_t data)
{
  uint8_t Line=data, i;
  OledSetPos(x,y);	
  for(i=0;i<8;i++)
  {
    WriteData(Fdata[Line][i]);
  }	
  OledSetPos(x,y+1);	
  for(i=0;i<8;i++)
  {	
    WriteData(Fdata[Line][i+8]);
  }			
}

//x:0~127:0~7
//Char_Size:16/12 
void OledShowChar(uint8_t x,uint8_t y,uint8_t chr,uint8_t Char_Size)
{      	
  uint8_t c=0,i=0;	
  
  c=chr-' ';			
  if(x>127-1)
  {
    x=0;
    y=y+2;
  }
  if(Char_Size ==16)
  {
    OledSetPos(x,y);	
    for(i=0;i<8;i++)
    {
      WriteData(F8X16[c*16+i]);
    }
    
    OledSetPos(x,y+1);
    for(i=0;i<8;i++)
    {
      WriteData(F8X16[c*16+i+8]);
    }
  }
  else
  {	
    OledSetPos(x,y);
    for(i=0;i<6;i++)
    {
      WriteData(F6x8[c][i]);
    }
  }
}
 
void OledShowString(uint8_t x,uint8_t y,char *str,uint8_t Char_Size)
{
  unsigned char j=0;
  
  while (str[j]!='\0')
  {
    OledShowChar(x,y,str[j],Char_Size);
    x+=8;
    if(x>120)
    {
      x=0;
      y+=2;
    }
    j++;//0-7
  }
}
 
void OledShowCN(uint8_t x,uint8_t y,uint8_t index)
{      			    
  uint8_t t;
	
  OledSetPos(x,y);	
  for(t=0;t<16;t++)
  {
    WriteData(Hzk[index][t]);
  }	
  
  OledSetPos(x,y+1);	
  for(t=0;t<16;t++)
  {	
    WriteData(Hzk[index][t+16]);
  }					
}

void OledInit(void)
{
  unsigned char i;
  for(i=0;i<9;i++)
  {
    delay_nop(100000);//3.3ms
  } 
  WriteCmd(0xAE);//--display off/*ChengXubuguo*/
  WriteCmd(0x00);//--set low column address
  WriteCmd(0x10);//--set high column address
  WriteCmd(0x40);//--set start line address  
  WriteCmd(0xB0);//--set page address
  WriteCmd(0x81);// contract control
  WriteCmd(0xFF);//--128   
  WriteCmd(0xA1);//set segment re-map  0 to 127 
  WriteCmd(0xA6);//set normal  display  ///
  WriteCmd(0xA8);//set multiplex ratio(1 to 64)
  WriteCmd(0x3F);//--1/32 duty
  WriteCmd(0xC8);//Com scan direction
  WriteCmd(0xD3);//set display offset
  WriteCmd(0x00);//no offset
  WriteCmd(0xD5);//set display clock divide ratio/oscillator frequency  
  WriteCmd(0x80);//
  WriteCmd(0xD8);//set area color mode off
  WriteCmd(0x05);//
  WriteCmd(0xD9);//Set Pre-Charge Period
  WriteCmd(0xF1);//
  WriteCmd(0xDA);//set com pin  hardware configuartion
  WriteCmd(0x12);//
  WriteCmd(0xDB);//set Vcomh
  WriteCmd(0x30);//0x20,0.77xVcc
  WriteCmd(0x8D);//set charge pump enable
  WriteCmd(0x14);//
  WriteCmd(0xAF);//--turn on Oled panel
}

void OledShow()
{
  OledShowCN(0,0,8);
	OledShowCN(0,1,9);
	OledShowCN(16,0,10);
	OledShowCN(16,1,11);
	OledShowCN(31,0,12);
	OledShowCN(31,1,13);
/*
	OledShowCN(40,0,14);
	OledShowCN(40,1,15);
	OledShowCN(48,0,16);
	OledShowCN(48,1,17);
*/
	OledShowCN(56,0,18);
	OledShowCN(56,1,19);//.
/*
	OledShowCN(64,0,20);
	OledShowCN(64,1,21);
*/  
	OledShowCN(94,0,22);//74
	OledShowCN(94,1,23);//v

	OledShowCN(0,2,24);
	OledShowCN(0,3,25);
	OledShowCN(16,2,26);
	OledShowCN(16,3,27);
	OledShowCN(31,2,28);
	OledShowCN(31,3,29);
/*
	OledShowCN(40,2,14);
	OledShowCN(40,3,15);
	OledShowCN(48,2,16);
	OledShowCN(48,3,17);
*/
	OledShowCN(56,2,18);
	OledShowCN(56,3,19);//.
/*
	OledShowCN(64,2,20);
	OledShowCN(64,3,21);
*/
	OledShowCN(92,2,30);
	OledShowCN(92,3,31);//u
	OledShowCN(100,2,32);
	OledShowCN(100,3,33);//A

	OledShowCN(0,4,34);
	OledShowCN(0,5,35);
	OledShowCN(16,4,36);
	OledShowCN(16,5,37);
	OledShowCN(31,4,38);
	OledShowCN(31,5,39);
/*
	OledShowCN(40,4,14);
	OledShowCN(40,5,15);
	OledShowCN(48,4,16);
	OledShowCN(48,5,17);
*/
	OledShowCN(56,4,18);
	OledShowCN(56,5,19);//.
/*
	OledShowCN(64,4,20);
	OledShowCN(64,5,21);
*/
	OledShowCN(92,4,40);
	OledShowCN(92,5,41);//a
	OledShowCN(100,4,42);
	OledShowCN(100,5,43);//b

}

void OledDisplayTest(void)
{
	I2cInit();
	OledInit();
	
	OledClear(); 

	OledShowString(28,0,(char *)("EEWORLD"),16);
	OledShowString(40,2,(char *)("GD32"),16);
	OledShowString(40,4,(char *)("2022"),16);

	OledShowCN(24,6,0);
	OledShowCN(40,6,1);
	OledShowCN(56,6,2);
	OledShowCN(72,6,3);
}
